package com.xnyzc.lhy.task.fallback.mis;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.component.sms.AliCloudSMSUtils;
import com.xnyzc.lhy.common.constant.AliCloudSmsConstant;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.order.EOrderChannelType;
import com.xnyzc.lhy.common.penum.vehicle.ECallVehType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.mis.entity.manager.OaSysManager;
import com.xnyzc.lhy.mis.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.mis.entity.system.OaSysSendMessage;
import com.xnyzc.lhy.task.fegin.mis.QuartzCOrderTaskService;
import com.xnyzc.lhy.task.mapper.sys.OaCOrderPersonalMapper;
import com.xnyzc.lhy.task.mapper.sys.OaSysSendMessageMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class QuartzCOrderTaskServiceFallback implements QuartzCOrderTaskService {
    @Autowired
    private RedisCommon redisCommon;
    @Autowired
    private OaCOrderPersonalMapper oaCOrderPersonalMapper;
    @Autowired
    private OaSysSendMessageMapper oaSysSendMessageMapper;

    @Override
    public void managerOrderMSG() {
        //获取到使用时间未指派司机订单
        QueryWrapper<OaCOrderPersonal> personalQueryWrapper = new QueryWrapper<>();
        personalQueryWrapper.eq(OaCOrderPersonal.ORDER_STATUS, 0);//等待应答状态
        personalQueryWrapper.eq(OaCOrderPersonal.DELETED, 0);
        personalQueryWrapper.isNull(OaCOrderPersonal.DRIVER_ID);
        List<OaCOrderPersonal> oaCOrderPersonalList = oaCOrderPersonalMapper.selectList(personalQueryWrapper);
        if (oaCOrderPersonalList.size() <= 0) {
            return;
        }
        for (OaCOrderPersonal info : oaCOrderPersonalList) {
            //乘客订单的使用时间与当前时间比较
            long now = System.currentTimeMillis();
            long cUseTime = info.getAppointment().getTime();
            //2个半小时
            long difTime = 1000 * 60 * 60 * 2 + 1000 * 60 * 30;
            //运算结果不能为负数,为负数说明预约时间小于当前时间
            if (cUseTime - now <= difTime && cUseTime - now > 0) {
                Integer cityId = Integer.parseInt(info.getCityId());
                //获取该城市的司管电话
                QueryWrapper<OaSysSendMessage> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq(OaSysSendMessage.OFFICE_ID, info.getOfficeId());
                OaSysSendMessage oaSysSendMessage = oaSysSendMessageMapper.selectOne(queryWrapper);
                if (CheckUtil.objIsNotEmpty(oaSysSendMessage)&& oaSysSendMessage.getUndeliveredEarlyWarning()==1) {
                    JSONObject json = new JSONObject();
                    String orderNo = info.getOrderNo();
                    int orderNoLength = orderNo.length();
                    json.put("orderId", orderNo);
                    if (orderNoLength > 20) {
                        json.put("orderId", orderNo.substring(orderNoLength - 20, orderNoLength));
                    }
                    json.put("channel", EOrderChannelType.findById(info.getOrderChannel()).getValue());
                    json.put("calltype", ECallVehType.finaByType(info.getOrderType()).getValue());
                    //发送短信给司管
                    String redisKey = RedisPrefixConstant.MANAGER_ORDER_MSG + info.getOrderNo();
                    //从redi获取数据
                    String sendMessage = redisCommon.getStr(redisKey);
                    if (CheckUtil.strIsEmpty(sendMessage)) {
                        //数据为空 发送短信
                        AliCloudSMSUtils.sendMessage(oaSysSendMessage.getManagerPhone().toString(), AliCloudSmsConstant.MANAGER_ORDER_MSG, json);
                        redisCommon.set(redisKey, "1", (long) (1000 * 60 * 60 * 2.5));
                        log.info("成功发送短信给司管！ 电话：" + oaSysSendMessage.getManagerPhone());
                    } else {//数据不为空 已经发送过
                        log.info("已发送司管短信订单号:" + orderNo);
                    }
                } else {
                    log.info("该供应商未配置司管电话：{}", info.getOfficeId());
                }
            }
        }
    }
}
